package cn.edu.pku.tools;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileHelper
{
  public String FILESPATH;
  public String SDPATH;
  public String appFilePath;
  public Context context;

  public FileHelper(Context paramContext)
  {
    this.context = paramContext;
    this.SDPATH = (Environment.getExternalStorageDirectory().getPath() + "//");
    creatSDDir("YiJiaRen");
    creatSDDir("YiJiaRen/pic");
    this.appFilePath = (this.SDPATH + "YiJiaRen" + "/");
    this.FILESPATH = (this.context.getFilesDir().getPath() + "//");
  }

  public Output appendFile(String paramString)
    throws IOException
  {
    return new Output(this.context.openFileOutput(paramString, 32768));
  }

  public Output appendSDFile(String paramString)
    throws IOException
  {
    return new Output(new FileOutputStream(new File(this.SDPATH + paramString), true));
  }

 
  public File creatDataDir(String paramString)
  {
    File localFile = new File(this.FILESPATH + paramString);
    localFile.mkdir();
    return localFile;
  }

  public File creatDataFile(String paramString)
    throws IOException
  {
    File localFile = new File(this.FILESPATH + paramString);
    localFile.createNewFile();
    return localFile;
  }

  public File creatSDDir(String paramString)
  {
    File localFile = new File(this.SDPATH + paramString);
    if (!localFile.exists())
      localFile.mkdir();
    return localFile;
  }

  public File creatSDFile(String paramString)
    throws IOException
  {
    File localFile = new File(this.SDPATH + paramString);
    localFile.createNewFile();
    return localFile;
  }

 
  

  public boolean isSDFileExit(String paramString)
  {
    return new File(this.appFilePath + paramString).exists();
  }

  
  public Input readFile(String paramString)
    throws IOException
  {
    return new Input(this.context.openFileInput(paramString));
  }

  public Bitmap readPic(String paramString)
  {
    Bitmap localBitmap = null;
    String str = this.appFilePath + paramString;
    if (new File(str).exists())
      localBitmap = BitmapFactory.decodeFile(str);
    return localBitmap;
  }

  public Input readSDFile(String paramString)
    throws IOException
  {
    return new Input(new FileInputStream(new File(this.SDPATH + paramString)));
  }

  public boolean renameDataFile(String paramString1, String paramString2)
  {
    return new File(this.FILESPATH + paramString1).renameTo(new File(this.FILESPATH + paramString2));
  }

  public boolean renameSDFile(String paramString1, String paramString2)
  {
    return new File(this.SDPATH + paramString1).renameTo(new File(this.SDPATH + paramString2));
  }

  public Output wirteFile(String paramString)
    throws IOException
  {
    return new Output(this.context.openFileOutput(paramString, 2));
  }

  public Output writeSDFile(String paramString1, String paramString2)
    throws IOException
  {
    creatSDDir("YiJiaRen/" + paramString1);
    return new Output(new FileOutputStream(new File(this.appFilePath + "/" + paramString1 + "/" + paramString2)));
  }
  
  
  public void copyFile(String oldPath, String newPath) {   
      try {   
          int bytesum = 0;   
          int byteread = 0;   
          File oldfile = new File(oldPath);   
          if (!oldfile.exists()) { //文件不存在时   
              InputStream inStream = new FileInputStream(oldPath); //读入原文件   
              FileOutputStream fs = new FileOutputStream(newPath);   
              byte[] buffer = new byte[1444];   
              int length;   
              while ( (byteread = inStream.read(buffer)) != -1) {   
                  bytesum += byteread; //字节数 文件大小   
                  System.out.println(bytesum);   
                  fs.write(buffer, 0, byteread);   
              }   
              inStream.close();   
          }   
      }   
      catch (Exception e) {   
          System.out.println("复制单个文件操作出错");   
          e.printStackTrace();   
 
      }   
 
  }   
}